home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 22 / AACD 22.iso / AACD / Online / Apache / lib / php / DB / storage.php < prev    next >
Encoding:
PHP Script  |  2001-03-06  |  8.0 KB  |  274 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group                   |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <ssb@fast.no>                                   |
  17. // |                                                                      |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // DB_storage: a class that lets you return SQL data as objects that
  21. // can be manipulated and that updates the database accordingly.
  22. //
  23.  
  24. require_once "DB.php";
  25.  
  26. function DB_storage_destructor() {
  27.     global $DB_storage_object_list;
  28.  
  29.     if (is_array($DB_storage_object_list)) {
  30.     reset($DB_storage_object_list);
  31.     while (list($ind, $obj) = each($DB_storage_object_list)) {
  32.         $obj->destroy();
  33.     }
  34.     $DB_storage_object_list = null;
  35.     }
  36. }
  37.  
  38. class DB_storage {
  39.     /** the name of the table (or view, if the backend database supports
  40.         updates in views) we hold data from */
  41.     var $_table = null;
  42.     /** which column in the table contains primary keys */
  43.     var $_keycolumn = null;
  44.     /** DB connection handle used for all transactions */
  45.     var $_dbh = null;
  46.     /** an assoc with the names of database fields stored as properties
  47.     in this object */
  48.     var $_properties = array();
  49.     /** an assoc with the names of the properties in this object that
  50.     have been changed since they were fetched from the database */
  51.     var $_changes = array();
  52.     /** flag that decides if data in this object can be changed.
  53.     objects that don't have their table's key column in their
  54.     property lists will be flagged as read-only. */
  55.     var $_readonly = false;
  56.  
  57.     /**
  58.      * Constructor, adds itself to the DB_storage class's list of
  59.      * objects that should have their "destroy" method called when
  60.      * PHP shuts down (poor man's destructors).
  61.      */
  62.     function DB_storage($table, $keycolumn, &$dbh) {
  63.     global $DB_storage_object_list;
  64.     if (is_array($DB_storage_object_list)) {
  65.         $DB_storage_object_list[] = &$this;
  66.     } else {
  67.         $DB_storage_object_list = array(&$this);
  68.     }
  69.     $this->_table = $table;
  70.     $this->_keycolumn = $keycolumn;
  71.     $this->_dbh = $dbh;
  72.     $this->_readonly = false;
  73.     }
  74.  
  75.     /**
  76.      * Method used to initialize a DB_storage object from the
  77.      * configured table.
  78.      * @param $keyval the key of the row to fetch
  79.      * @return int DB_OK on success, DB error if not
  80.      */
  81.     function setup($keyval) {
  82.     if (is_int($keyval)) {
  83.         $qval = "$keyval";
  84.     } else {
  85.         $qval = "'" . $this->_dbh->quoteString($keyval) . "'";
  86.     }
  87.     $sth = $this->_dbh->query("SELECT * FROM " .
  88.                   $this->_table . " WHERE " .
  89.                   $this->_keycolumn . " = $qval");
  90.     if (DB::isError($sth)) {
  91.         return $sth;
  92.     }
  93.     while ($row = $sth->fetchRow(DB_FETCHMODE_ASSOC)) {
  94.         reset($row);
  95.         while (list($key, $value) = each($row)) {
  96.         $this->_properties[$key] = true;
  97.         $this->$key = &$value;
  98.         unset($value);
  99.         }
  100.     }
  101.     }
  102.  
  103.     /**
  104.      * Create a new (empty) row in the configured table for this
  105.      * object.
  106.      */
  107.     function insert($newid = false) {
  108.     if (is_int($newid)) {
  109.         $qid = "$newid";
  110.     } else {
  111.         $qid = "'" . $this->_dbh->quoteString($newid) . "'";
  112.     }
  113.     $sth = $this->_dbh->query("INSERT INTO " .
  114.                   $this->_table . " (" .
  115.                   $this->_keycolumn .
  116.                   ") VALUES($qid)");
  117.     if (DB::isError($sth)) {
  118.         return $sth;
  119.     }
  120.     $this->setup($newid);
  121.     }
  122.  
  123.     /**
  124.      * Output a simple description of this DB_storage object.
  125.      * @return string object description
  126.      */
  127.     function toString() {
  128.     $info = get_class(&$this);
  129.     $info .= " (table=";
  130.     $info .= $this->_table;
  131.     $info .= ", keycolumn=";
  132.     $info .= $this->_keycolumn;
  133.     $info .= ", dbh=";
  134.     if (is_object($this->_dbh)) {
  135.         $info .= $this->_dbh->toString();
  136.     } else {
  137.         $info .= "null";
  138.     }
  139.     $info .= ")";
  140.     if (sizeof($this->_properties)) {
  141.         $keyname = $this->_keycolumn;
  142.         $key = $this->$keyname;
  143.         $info .= " [loaded, key=$key]";
  144.     }
  145.     if (sizeof($this->_changes)) {
  146.         $info .= " [modified]";
  147.     }
  148.     return $info;
  149.     }
  150.  
  151.     /**
  152.      * Dump the contents of this object to "standard output".
  153.      */
  154.     function dump() {
  155.     reset($this->_properties);
  156.     while (list($prop, $foo) = each($this->_properties)) {
  157.         print "$prop = ";
  158.         print htmlentities($this->$prop);
  159.         print "<BR>\n";
  160.     }
  161.     }
  162.  
  163.     /**
  164.      * Static method used to create new DB storage objects.
  165.      * @param $data assoc. array where the keys are the names
  166.      *              of properties/columns
  167.      * @return object a new instance of DB_storage or a subclass of it
  168.      */
  169.     function &create($table, &$data) {
  170.     $classname = get_class(&$this);
  171.     $obj = new $classname($table);
  172.     reset($data);
  173.     while (list($name, $value) = each($data)) {
  174.         $obj->_properties[$name] = true;
  175.         $obj->$name = &$value;
  176.     }
  177.     return $obj;
  178.     }
  179.  
  180.     /**
  181.      * Loads data into this object from the given query.  If this
  182.      * object already contains table data, changes will be saved and
  183.      * the object re-initialized first.
  184.      *
  185.      * @param $query SQL query
  186.      *
  187.      * @param $params parameter list in case you want to use
  188.      * prepare/execute mode
  189.      *
  190.      * @return int DB_OK on success, DB_WARNING_READ_ONLY if the
  191.      * returned object is read-only (because the object's specified
  192.      * key column was not found among the columns returned by $query),
  193.      * or another DB error code in case of errors.
  194.      */
  195.     function loadFromQuery($query, $params = false) {
  196.     if (sizeof($this->_properties)) {
  197.         if (sizeof($this->_changes)) {
  198.         $this->store();
  199.         $this->_changes = array();
  200.         }
  201.         $this->_properties = array();
  202.     }
  203.     $rowdata = $this->_dbh->getRow($query, DB_FETCHMODE_ASSOC, $params);
  204.     if (DB::isError($rowdata)) {
  205.         return $rowdata;
  206.     }
  207.     reset($rowdata);
  208.     $found_keycolumn = false;
  209.     while (list($key, $value) = each($rowdata)) {
  210.         if ($key == $this->_keycolumn) {
  211.         $found_keycolumn = true;
  212.         }
  213.         $this->_properties[$key] = true;
  214.         $this->$key = &$value;
  215.         unset($value); // have to unset, or all properties will
  216.                    // refer to the same value
  217.     }
  218.     if (!$found_keycolumn) {
  219.         $this->_readonly = true;
  220.         return DB_WARNING_READ_ONLY;
  221.     }
  222.     return DB_OK;
  223.     }
  224.  
  225.     function set($property, &$newvalue) {
  226.     // only change if $property is known and object is not
  227.     // read-only
  228.     if (!$this->_readonly && isset($this->_properties[$property])) {
  229.         $this->$property = $newvalue;
  230.         $this->_changes[$property]++;
  231.         return true;
  232.     }
  233.     return false;
  234.     }
  235.  
  236.     function &get($property) {
  237.     // only return if $property is known
  238.     if (isset($this->_properties[$property])) {
  239.         return $this->$property;
  240.     }
  241.     return null;
  242.     }
  243.  
  244.     function destroy($discard = false) {
  245.     if (!$discard && sizeof($this->_changes)) {
  246.         $this->store();
  247.     }
  248.     $this->_properties = array();
  249.     $this->_changes = array();
  250.     $this->_table = null;
  251.     }
  252.  
  253.     function store() {
  254.     while (list($name, $changed) = each($this->_changes)) {
  255.         $params[] = &$this->$name;
  256.         $vars[] = $name . ' = ?';
  257.     }
  258.     if ($vars) {
  259.         $query = 'UPDATE ' . $this->_table . ' SET ' .
  260.         implode(', ', $vars) . ' WHERE id = ?';
  261.         $params[] = $this->id;
  262.         $stmt = $this->_dbh->prepare($query);
  263.         $res = $this->_dbh->execute($stmt, &$params);
  264.         if (DB::isError($res)) {
  265.         return $res;
  266.         }
  267.         $this->_changes = array();
  268.     }
  269.     return DB_OK;
  270.     }
  271. }
  272.  
  273. ?>
  274.